<template>
  <div>

    <div class="ke__panel">
      <div class="ke__panel-head">
        <el-button size="small" @click="$router.back()">
          返回
        </el-button>

        <el-button size="small" :loading="subing" type="primary" @click="$refs.form.onSubmit()">
          <i class="icon icon-save" style="font-size: 12px"/>
          保存
        </el-button>
      </div>

      <div class="ke__panel-body">
        <k-form class="form-panel"
                ref="form"
                v-loading="loading"
                @submit="onSubmit"
                label-width="180px">
          <k-form-item label="用户组名称">
            <el-input v-model="form.name"></el-input>
          </k-form-item>

          <k-form-item label="备注">
            <el-input v-model="form.remark" type="textarea" rows="5"></el-input>
          </k-form-item>
        </k-form>
      </div>
    </div>
  </div>
</template>

<script>
import { Vue, Component } from 'vue-property-decorator'
@Component
export default class SafeGroupEdit extends Vue {
  loading = false
  subing = false
  form = {}

  get id() {
    let val = parseInt(this.$route.params.id)
    if (isNaN(val)) return 0
    return val
  }

  mounted () {
    if (this.id) {
      this.getData(this.id)
    }
  }

  // 获取管理信息
  getData (id) {
    this.loading = true
    this.$http.get(`admin/safe/group/${id}`)
      .finally(() => {
        this.loading = false
      })
      .then(result => {
        if (result.res_code === 0) {
          this.form = result.data
        } else {
          this.$message.error(result.res_msg)
          this.$router.back()
        }
      })
  }

  onSubmit () {
    this.subing = true
    this.$http.post('admin/safe/group', this.form)
      .finally(() => {
        this.subing = false
      })
      .then(result => {
        if (result.res_code === 0) {
          this.$message.success('修改成功')
          this.$router.back()
        } else {
          this.$message.error(result.res_msg)
        }
      })
  }
}
</script>

<style scoped>

</style>
